package com.zty.product.controller;

import com.zty.product.Vo.ProductInfoVO;
import com.zty.product.Vo.ProductVO;
import com.zty.product.Vo.ResultVo;
import com.zty.product.dataobject.ProductCategory;
import com.zty.product.dataobject.ProductInfo;
import com.zty.product.service.ProductCategroyService;
import com.zty.product.service.ProductInfoService;
import com.zty.product.utils.ResultVoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *@Author:zty
 *@Description:
 *@Date:2018-9-13 17:58
 *@param:
 *@return:
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductCategroyService productCategroyService;
    /**
     * 1. 查询所有在架的商品
     * 2. 获取类目type列表
     * 3. 查询类目
     * 4. 构造数据
     */
    @GetMapping("/list")
    public  ResultVo<ProductVO> list(){
        List<ProductInfo> list = productInfoService.findUpAll();

        List<Integer> CategoryTypeList = list.stream().map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());

        List<ProductCategory> CategoryList = productCategroyService.findByCategoryTypeIn(CategoryTypeList);

        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory category : CategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(category.getCategoryName());
            productVO.setCategoryType(category.getCategoryType());
            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : list){
                if (productInfo.getCategoryType().equals(category.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
          return    ResultVoUtil.success(productVOList);
    }
}
